package com.szholly.pro.watergas_server.ps.android.controller;

import java.util.ArrayList;
import java.util.List;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class PipelinesPointManager {
	private JSONObject errorJson;

	public String getMapPointData(String type, String params) {
		if (errorJson == null) {
			errorJson = new JSONObject();
		} else {
			errorJson.clear();
		}
		errorJson.put("error", "类型有误");
		errorJson.put("data", "[]");
		if (type == null || type.isEmpty()) {
			return errorJson.toString();
		}
		String result =null;
		if ("排水管网".equals(type)) {

		} else if ("污水厂".equals(type)) {

		} else if ("管网监测点".equals(type)) {

		} else if ("规划管网".equals(type)) {
			
		} else if ("清源小区".equals(type)) {

		} else if ("排水管网设施".equals(type)) {

		} else if ("内窥检测".equals(type)) {
			result = getEndoscopicData(params);
		}
		if (result == null) {
			result = errorJson.toString();
		}
		return result;
	}

	private String getEndoscopicData(String paramsJsonStr) {
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		String error = "";
		String sql = null;
		JSONArray resultArray = null;
		try {
			JSONObject paramsJson = JSONObject.fromObject(paramsJsonStr);
			String type = paramsJson.optString("type", "");
			if ("缺陷id".equals(type)) {
				// 表示进行内窥缺陷id查找
				sql = "select t.objectid,t.LNO,t.ENAME name,t4.PROJECT_DEPT address,'流向：' || t1.FLOW || ' 缺陷等级：' || case t1.DEFECT_LEVEL when '1' then '低' when '2' then '中' when '3' then '高' when '4' then '严重' else '无缺陷' end || ' 管径：' || t1.PIPE_DIAMETER tip,t.shape.minx x,t.shape.miny y, sde.st_astext(t.shape) wkt,t5.S_DOMAINCAPTION from sde.JCGW t  LEFT JOIN nkjc_defect t1 ON t.lno = t1.lno  left join PIPE_SECTION t2 on t1.pipe_no=t2.pipe_no left join project_item t3 on t2.project_item_no=t3.item_no left join PROJECT t4 on t3.project_no=t4.project_no left join(select s_id,S_DOMAINCAPTION from DATA_DEFULATDOMAIN where S_DOMAINNAME = '政区' and length(s_id)='2') t5 on t5.s_id =t3.DISTRICT WHERE 1 = 1";
				String lno = paramsJson.optString("value1");
				if (lno != null && !lno.isEmpty()) {
					sql += " AND t.LNO = '" + lno + "'";
				}
				List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
				resultArray = JSONArray.fromObject(rows);
			}else if ("属性复核id".equals(type)) {
				sql = "select t.objectid,t.LNO,t.ENAME name,t4.PROJECT_DEPT address,'流向：' || t1.FLOW || ' 缺陷等级：' || case t1.DEFECT_LEVEL when '1' then '低' when '2' then '中' when '3' then '高' when '4' then '严重' else '无缺陷' end || ' 管径：' || t1.PIPE_DIAMETER tip,t.shape.minx x,t.shape.miny y, sde.st_astext(t.shape) wkt,t5.S_DOMAINCAPTION from sde.JCGW t  LEFT JOIN nkjc_defect t1 ON t.lno = t1.lno  left join PIPE_SECTION t2 on t1.pipe_no=t2.pipe_no left join project_item t3 on t2.project_item_no=t3.item_no left join PROJECT t4 on t3.project_no=t4.project_no left join(select s_id,S_DOMAINCAPTION from DATA_DEFULATDOMAIN where S_DOMAINNAME = '政区' and length(s_id)='2') t5 on t5.s_id =t3.DISTRICT WHERE 1 = 1";
				String id = paramsJson.optString("value1");
				if (id != null && !id.isEmpty()) {
					sql += " AND t.OBJECTID = '" + id + "'";
				}
				List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
				resultArray = JSONArray.fromObject(rows);
			}
		} catch (Exception e) {
			error += e.getMessage() + "|";
		}
		errorJson.put("error", error);
		if (resultArray == null) {
			errorJson.put("data", "[]");
		}else{
			errorJson.put("data", resultArray);
		}
		return errorJson.toString();
	}
}
